<?php

use swap\service_except;

class resume_ext_except extends service_except {
    
    public static $except_msgs = array(
        'language' => '语言能力最多只能添加%d个',
        'training' => '培训经历最多只能添加%d个',
        'certificate' => '证书最多只能添加%d个',
    );
    
}

class resume_ext_service extends swap\service {
    
    // note:本来相关资源对应的limit应该定义在各自的service中，但是这里这样做是为了集中管理。
    public static $limits = array(
        'language' => 8,
        'training' => 3,
        'certificate' => 8,
    );
    
    /**
     * (1, 'myself')
     * 
     * $ext_data = array(
     *  
     * )
     */
    public static function get_resume_ext($resume_id, $type) {
        $resume = resume_ext_model::get_one(array('resume_id' => $resume_id));
        if ($resume === null) {
            return null;
        }
        $type_field = $type . '_id';
        return $resume->$type_field === '' ? null : json_decode($resume->$type_field);
    }
    /**
     * (1, 'language', 1)
     * (1, 'language', 2)
     * 
     * => language_id => array(1, 2)
     * 
     * (1, 'myself', 1)
     * => myself_id => array(1)
     * 
     * (1, 'intention', 1)
     * (1, 'intention', 2)
     * => intention_id => array(1, 2)
     * 
     */
    public static function set_resume_ext($resume_id, $type, $type_id) {
        
        $resume_ext = resume_ext_model::get_one(array('resume_id' => $resume_id));
        if ($resume_ext === null) {
            return null;
        }
        $type_field = $type . '_id';
        $type_id = (int)$type_id;
        // 处理数据
        $value = $resume_ext->$type_field;
        
        if ($value !== '') {
            $value = json_decode($value);
            if (!in_array($type_id, $value)) {
                array_push($value, $type_id);
            }
        } else {
            $value = array($type_id);
            
            // 更新分数
            resume_model::inc_resume_degree($resume_id, $type);
        }
        // 更新数据
        resume_ext_model::set_by_id(array($type_field => json_encode($value)), $resume_ext->id);
    }
    public static function del_resume_ext($resume_id, $type, $type_id) {
        // 记录是否存在
        $resume_ext = resume_ext_model::get_one(array('resume_id' => $resume_id));
        if ($resume_ext === null) {
            return null;
        }
        
        // 数据判断
        $type_field = $type . '_id';
        $type_id = (int)$type_id;
        $value = $resume_ext->$type_field;
        if ($value === '') {
            return null;
        }
        $value = json_decode($value);
        if (!in_array($type_id, $value)) {
            return null;
        }
        $key = array_search($type_id, $value);
        unset($value[$key]);
        
        // 分数
        $length = count($value);
        if ($length === 0) {
            resume_model::dec_resume_degree($resume_id, $type);
        }
        
        // 更新resume_ext记录
        if ($value === array()) {
            $value = '';
        } else {
            sort($value);
            $value = json_encode($value);
        }
        resume_ext_model::set_by_id(array($type_field => $value), $resume_ext->id);
        
    }
    
    /**
     * 检查是否可以设置resume_ext
     */
    public static function can_set_resume_ext($resume_id, $type) {
        if (!array_key_exists($type, self::$limits)) {
            resume_ext_except::throw_prop_msg($type, '未知的简历类型');
        }
        
        $type_number = count(self::get_resume_ext($resume_id, $type));
        if ($type_number >= self::$limits[$type]) {
            resume_ext_except::throw_prop_msg($type, sprintf(resume_ext_except::$except_msgs[$type], self::$limits[$type]));
        }
    }

}